package com.thinkfit.jy.utils;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

public class LayoutTouchListenerUtils {

    private static final String TAG = "LayoutTouchListenerUtils ------------------->";
    static LayoutTouchListenerUtils layoutTouchListenerUtils;
    View mView;
    float posX,posY,curPosX,curPosY;

    public static LayoutTouchListenerUtils getInstance(){
        if(layoutTouchListenerUtils == null){
            layoutTouchListenerUtils = new LayoutTouchListenerUtils();
        }

        return layoutTouchListenerUtils;
    }

    public void setLayoutTouchListener(Context context, View view){
        mView = view;
        mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        posX = event.getX();
                        posY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        curPosX = event.getX();
                        curPosY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        if(posX<10 || posX > 1900){
                            if ((curPosX - posX > 0) && (Math.abs(curPosX - posX) > 25)){
                                Log.v(TAG,"向左滑动");
                                ToastUtils.showShort("向左滑动");
//                                context.onBackPressed();
                            } else if ((curPosX - posX < 0) && (Math.abs(curPosX-posX) > 25)){
                                Log.v(TAG,"向右滑动");
                                ToastUtils.showShort("向右滑动");
//                                Bundle bundle = new Bundle();
//                                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
//                                Intent intent = new Intent(context, SettingActivity.class);
//                                intent.putExtras(bundle);
//                                context.startActivity(intent);
//                                context.overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
                            }
                        }else if(posY <10 || posY >1060){
                            if ((curPosY - posY > 0) && (Math.abs(curPosY - posY) > 25)){
                                Log.v(TAG,"向下滑动");
                                ToastUtils.showShort("向下滑动");
                            }
                            else if ((curPosY - posY < 0) && (Math.abs(curPosY-posY) > 25)){
                                Log.v(TAG,"向上滑动");
                                ToastUtils.showShort("向上滑动");
                            }
                        }
                        break;
                }
                return true;
            }
        });
    }

    public void cleanListener(){
        mView.setOnTouchListener(null);
    }
}
